package game.view.scene
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import game.common.MapAssetManager;
	import game.controller.GameController;
	import game.events.GamePlayerEvent;
	import game.events.SceneEvent;
	import game.model.GameModel;
	import game.model.SceneModel;
	import game.view.player.GamePlayerBase;
	import game.vo.GamePlayerVO;
	import game.vo.MapAssetVO;
	
	import helper.URLLoaderHelper;
	
	import iso.IsoGrid;
	import iso.IsoPoint;
	import iso.IsoScene;

	public class MapSceneView extends Sprite
	{
		private var _controller:GameController;
		private var _model:GameModel;
		private var _sceneModel:SceneModel;
		private var _mapScene:SceneView;//地图场景层
		private var _mapGrid:IsoGrid;//地图网格层
		private var _selfPlayer:GamePlayerBase
		
		public function MapSceneView(controller:GameController, model:GameModel, sceneModel:SceneModel)
		{
			_controller=controller;
			_model=model;
			_sceneModel=sceneModel;
			setView();
			setEvent();
			_sceneModel.mapConfigPath="map/map1/map.xml";//临时使用
		}
		
		private function setView():void
		{
			_mapScene=new SceneView(true, true);
			addChild(_mapScene);
			
			_mapGrid=new IsoGrid(_sceneModel.sceneVO.mapType, _sceneModel.sceneVO.cellSize, _sceneModel.sceneVO.mapRow, _sceneModel.sceneVO.mapCols, 0x999999, 0.2);
			_mapGrid.alpha=0.0;
			addChild(_mapGrid);
		}
		
		private function setEvent():void
		{
			_mapGrid.addEventListener(MouseEvent.CLICK, __onScreenClick);
			_sceneModel.addEventListener(SceneEvent.MAP_CONFIG_PATH_CHANGE, __onMapConfigPathChange);
			_sceneModel.sceneVO.addEventListener(SceneEvent.MAP_CONFIG_CHANGE, __onMapConfigChange);
			
//			addEventListener(MouseEvent.MOUSE_DOWN, __onMouseDown);
//			addEventListener(MouseEvent.MOUSE_UP, __onMouseUp);
		}
		
		private function removeEvent():void
		{
//			_mapGridLayer.removeEventListener(MouseEvent.CLICK, __onMapClick);
//			_sceneModel.removeEventListener(DemoMapEvent.MAP_CONFIG_PATH_CHANGE, __onMapConfigPathChange);
//			if(_selfPlayer) _selfPlayer.removeEventListener(DemoPlayerEvent.PLAYER_POINT_CHANGE, __onSelfPlayerPointChange);
//			if(_selfPlayer) _selfPlayer.removeEventListener(DemoPlayerEvent.PLAYER_GOTO_TARGET_POINT, __onPlayerGoToTargetPoint);
		}
		
		/**
		 *地图场景上左击 
		 */		
		private function __onScreenClick(evt:MouseEvent):void
		{
			var pt:Point=new Point(evt.localX, evt.localY);
			_selfPlayer.gamePlayerVO.targetPoint=pt;
		}
		
		/**
		 *当场景地图配置文件发生改变 
		 */
		private function __onMapConfigPathChange(evt:SceneEvent):void
		{
			var urlLoader:URLLoaderHelper=new URLLoaderHelper(_sceneModel.mapConfigPath, 0, __loadMapConfig);
			urlLoader.loadSync();
		}
		
		/**
		 *当场景地图配置文件发生改变 
		 */
		private function __loadMapConfig(data:*):void
		{
			_sceneModel.sceneVO.mapConfig=new XML(data);
		}
		
		/**
		 *当场景地图配置文件发生改变 
		 */
		private function __onMapConfigChange(evt:SceneEvent):void
		{
			var xml:XML=_sceneModel.sceneVO.mapConfig;
			
			//取得地图基本信息
			_sceneModel.sceneVO.mapName=xml.name;
			_sceneModel.sceneVO.mapType=int(xml.mapType);
			_sceneModel.sceneVO.mapDescription=xml.description;
			_sceneModel.sceneVO.mapRow=xml.row;
			_sceneModel.sceneVO.mapCols=xml.cols;
			_sceneModel.sceneVO.cellSize=xml.size;
			
			var assetXml:XML=XML(xml.map.asset);
			
			//设置资源库信息
			var arrLib:Array=assetXml.@assetLibPath.toString().split(',');
			_sceneModel.sceneVO.assetLibPath=[];
			for(var i:int=0;i<arrLib.length;i++)
			{
				_sceneModel.sceneVO.assetLibPath[i]=new Object();
				_sceneModel.sceneVO.assetLibPath[i].isLoad=false;//标识当前库是未加载的
				_sceneModel.sceneVO.assetLibPath[i].path=arrLib[i];//指定库路径
			}
			
			//清除原物件数据
			for(var m:int=0;m<_sceneModel.sceneVO.assetArray.length;m++)
			{
				if(_sceneModel.sceneVO.assetArray[m])
				{
					if(_sceneModel.sceneVO.assetArray[m].parent) _sceneModel.sceneVO.assetArray[m].parent.removeChild(_sceneModel.sceneVO.assetArray[m]);
					_sceneModel.sceneVO.assetArray[m].dispose();
				}
				_sceneModel.sceneVO.assetArray[m]=null;
			}
			
			//创建物件数据
			_sceneModel.sceneVO.assetArray=new Vector.<MapAssetBase>();
			var arrAsset:XMLList=assetXml.item;
			if(arrAsset && arrAsset.length()>0)
			{
				for(var x:int=0;x<arrAsset.length();x++)
				{
					var basePoint:Point=null;
					if(arrAsset[x].hasOwnProperty("@baseX") && arrAsset[x].hasOwnProperty("@baseY"))
					{
						basePoint=new Point(Number(arrAsset[x].@baseX), Number(arrAsset[x].@baseY));
					}
					
					var mab:MapAssetBase=new MapAssetBase(arrAsset[x].@link, false, basePoint);
					mab.mapAssetVO.assetName=arrAsset[x].@assetName;
					mab.mapAssetVO.isBottom=arrAsset[x].@isBottom=="true";
					mab.mapAssetVO.isFloor=arrAsset[x].@isFloor=="true";
					mab.mapAssetVO.isGrid=arrAsset[x].@isGrid=="true";
					mab.mapAssetVO.isMirror=arrAsset[x].@isMirror=="true";
					mab.mapAssetVO.link=arrAsset[x].@link;
					mab.position=new IsoPoint(arrAsset[x].@x, arrAsset[x].@y, arrAsset[x].@z);
					_sceneModel.sceneVO.assetArray.push(mab);
				}
			}
			
			loadMapAssetLib();
			
			//更新网格
			_mapGrid.gridType=_sceneModel.sceneVO.mapType;
			_mapGrid.rowSize=_sceneModel.sceneVO.mapRow;
			_mapGrid.colsSize=_sceneModel.sceneVO.mapCols;
			_mapGrid.cellSize=_sceneModel.sceneVO.cellSize;
			_mapGrid.update();
			
			var roadList:Array=xml.map.road.toString().split(',');
			_sceneModel.sceneVO.roadArray=[];
			for(i=0;i<_sceneModel.sceneVO.mapCols*2;i++)
			{
				_sceneModel.sceneVO.roadArray[i]=[];
				for(var c:int=0;c<=_sceneModel.sceneVO.mapRow;c++)
				{
					_sceneModel.sceneVO.roadArray[i][c]=roadList[0];
					roadList.shift();
				}
			}
			roadList=null;
		}
		
		/**
		 *加载地图资源物件库 
		 */		
		private function loadMapAssetLib():void
		{
			for each(var objLib:Object in _sceneModel.sceneVO.assetLibPath)
			{
				//加载资源库
				var mapAssetLoader:MapAssetManager=new MapAssetManager();
				mapAssetLoader.addEventListener(SceneEvent.MAP_ASSET_LOAD_COMPLETE, __onMapAssetLibLoadComplete);
				mapAssetLoader.loadConfig(objLib.path);
			}
		}
		
		/**
		 * 加载一个地图资源物件库完成
		 */		
		private function __onMapAssetLibLoadComplete(evt:SceneEvent):void
		{
			var mapAssetLoader:MapAssetManager=evt.currentTarget as MapAssetManager;
			mapAssetLoader.removeEventListener(SceneEvent.MAP_ASSET_LOAD_COMPLETE, __onMapAssetLibLoadComplete);
			setAssetLibPathLoadCompleteByPath(mapAssetLoader.path);
			mapAssetLoader.dispose();
			mapAssetLoader=null;
			
			if(checkAssetLibAllLoad())
			{//如果全部库资源已加载完成
				updateMapAsset();
			}
		}
		
		/**
		 *更新地图资源物件 
		 */		
		public function updateMapAsset():void
		{
			_mapScene.clearAll();
			
			//临时使用创建自已
			var gamePlayerVO:GamePlayerVO=new GamePlayerVO();
			_selfPlayer=new GamePlayerBase(gamePlayerVO, _sceneModel.sceneVO.roadArray, _sceneModel.sceneVO.cellSize);
			_selfPlayer.addEventListener(GamePlayerEvent.PLAYER_POINT_CHANGE, __onSelfPlayerPointChange);
			_selfPlayer.addEventListener(GamePlayerEvent.PLAYER_GOTO_TARGET_POINT, __onPlayerGoToTargetPoint);
			_selfPlayer.position=new IsoPoint(250, 250, 0);
			_mapScene.addPlayer(_selfPlayer);//增加玩家自已
			
			for each(var mab:MapAssetBase in _sceneModel.sceneVO.assetArray)
			{
				mab.loadAsset();
				if(mab.mapAssetVO.isFloor)
				{
					_mapScene.addToFloor(mab);
				}
				else
				{
					_mapScene.addToAsset(mab);
				}
			}
			
			_mapScene.drawFloor();
		}
		
		/**
		 *根据资源库路径，设置其已加载完成 
		 */		
		private function setAssetLibPathLoadCompleteByPath(path:String):void
		{
			for(var i:int=0;i<_sceneModel.sceneVO.assetLibPath.length;i++)
			{
				if(_sceneModel.sceneVO.assetLibPath[i].path==path)
				{
					_sceneModel.sceneVO.assetLibPath[i].isLoad=true;
				}
			}
		}
		
		/**
		 *检查库路径是否已经全部加载完成 
		 */		
		private function checkAssetLibAllLoad():Boolean
		{
			for(var i:int=0;i<_sceneModel.sceneVO.assetLibPath.length;i++)
			{
				if(_sceneModel.sceneVO.assetLibPath[i].isLoad==false) return false;
			}
			return true;
		}
		
		/**
		 *当前玩家自已坐标改变 
		 */		
		private function __onSelfPlayerPointChange(evt:GamePlayerEvent):void
		{
			setCenter();
			_mapScene.depthSort();
		}
		
		/**
		 *当前玩家已行动至目标坐标点 
		 */		
		private function __onPlayerGoToTargetPoint(evt:GamePlayerEvent):void
		{

		}
		
		/**
		 * 地图居中
		 */		
		private function setCenter():void
		{
			var xf : Number = -(_selfPlayer.x - stage.stageWidth / 2);
			var yf : Number = -(_selfPlayer.y - stage.stageHeight / 2)+50;
			
			if(xf > 0)xf = 0;
			if(xf < stage.stageWidth - _mapScene.width)
				xf = stage.stageWidth - _mapScene.width;
			if(yf > 0)yf = 0;
			if(yf < stage.stageHeight - _mapScene.height)
				yf = stage.stageHeight - _mapScene.height;
			
			if(xf>0) xf=0;
			if(yf>0) yf = 0;
			
			this.x = xf;
			this.y = yf;
		}
	}
}